var util = require('../../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
var startX = 0, endX = 0, prevScrollLeft = 0, prevId = 0, prevIndex = 0;
var px = 0, py = 0, px2 = 0, py2 = 0, windowWidth = 0;
Page({
  data: {
    hasData: false,
    middle_height: "100vh",
    dd_style: "",
    warehouse: '全部',
    warehouseId: '',
    delivery_date: util.dateObjectformat(new Date(), 'yyyy-MM-dd'),
    delivery_date_text: "今日签到",
    hasDispatch: false,
    hasOrderSignin: false,
    hasArrivalWarehouse: false,
    allDriver:0,
    hasSignin: 0,
    dialog: false
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onReady: function (e) {
  },
  onShow: function (e) {
    var that = this;
    wx.getStorage({
      key: 'monitor_warehouse',
      success: function (res) {
        if (res.data) {
          that.setData({
            warehouse: String(res.data.abbreviation || res.data.name).substr(0, 4),
            warehouseId: res.data.value
          });
          that.loadData();
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'monitor_warehouse',
          success: function (res) {
          }
        });
      }
    });
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--平台调度端" });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - 100,
          detail_height: res.windowHeight - 40
        });
        windowWidth = res.windowWidth;
      }
    });
    if (e.warehouseId) {
      that.setData({
        warehouse: e.warehouse,
        warehouseId: e.warehouseId
      });
    }
    this.loadData();
  },
  onShareAppMessage: function () {
    return {
      title: app.appName +'-司机出车签到管理',
      path: '/pages/schedule/orderSignin?warehouse=' + this.data.warehouse + '&warehouseId=' + this.data.warehouseId
    }
  },
  loadData: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.server + '/wechat/wxapplet/driverSigninList',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: {
        signin_date: that.data.delivery_date,
        warehouse: that.data.warehouseId
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res.data)
        if (res.data.success) {
          var _driverData = res.data.data[0], driverData = [],signinData = res.data.data[1];
          that.setData({
            allDriver: _driverData.length,
            hasSignin: signinData.length
          });
          for (var i = 0; i < _driverData.length; i++) {
            driverData.push({ plate: _driverData[i].plate, name: _driverData[i].name, phone: _driverData[i].phone, value: _driverData[i].ID, checked: false, ADD_TIME: '' });
          };
          for (var j = 0; j < signinData.length; j++) {
            for (var i = 0; i < driverData.length; i++) {
              if (driverData[i].value == signinData[j].driver){
                driverData[i].checked = signinData[j].signin_date ? true : false;
                driverData[i].ADD_TIME = signinData[j].ADD_TIME;
              }
            };
          }
          that.setData({
            hasDispatch: true,
            driverData: driverData
          });
        } else {
          that.setData({
            hasDispatch: false
          });
        }
      },
      fail: function (error) {
        wx.hideLoading();
        console.log(error);
        that.setData({
          hasDispatch: false
        })
      }
    });

  },
  pushDriverSignin:function(e){
    var that = this;
    if (that.data.warehouseId){
      wx.showLoading({
        title: '通知中...',
      })
      wx.showModal({
        title: '温馨提示',
        content: '确定要通知运力中的司机签到吗？?',
        success: function (res) {
          if (res.confirm) {
            util.postData("pushDriverSignin", { warehouse: that.data.warehouseId }, function (res, error) {
              console.log(res); console.log(error)
              wx.hideLoading();
              if (res.data.success) {
                wx.showToast({
                  title: '通知成功',
                  icon: 'success',
                  duration: 2000
                })
              }
            });
          }
        }
      })
    }else{
      wx.showToast({
        title: "需指定仓库",
        image: '/images/wrong.svg',
        duration: 2000
      });
    }
    
  },
  callDriver: function (e) {
    var phone = e.currentTarget.dataset.phone;
    var name = e.currentTarget.dataset.name;
    if (phone) {
      wx.showModal({
        title: '温馨提示',
        content: '确定要拨打电话给' + name+'?',
        success: function (res) {
          if (res.confirm) {
            wx.makePhoneCall({
              phoneNumber: phone
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: "暂无号码",
        image: '/images/wrong.svg',
        duration: 2000
      });
    }
  },
  changeDeliveryDate: function (e) {
    this.setData({
      delivery_date: e.detail.value,
      delivery_date_text: e.detail.value
    });
    this.loadData();
  },
  changeWarehouse: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/schedule/monitor_warehouse'
    })
  },
})
